/*
 * =====================================================================================
 *
 *       Filename:  cl_epoll_poller.hxx
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  03/30/2011 04:59:44 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Chen Ken (Ken), zlchen.ken@gmail.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#ifndef _CL_EPOLL_POLLER_HXX_
#define _CL_EPOLL_POLLER_HXX_

#include <sys/epoll.h>
#include <vector>
#include <map>
#include "cl_poller.hxx"

namespace neo
{
class CLEpollPoller: public CLPoller
{
public:
  CLEpollPoller();
  ~CLEpollPoller();
	int fd(void) const;

private:
  virtual bool doPoll(long millisecs, CLChannelList& chs);
	virtual bool doAdd(CLChannel* ch);
	virtual bool doRemove(CLChannel* ch);
	virtual bool doModify(CLChannel* ch);
  bool updateEpollfd(CLChannel* ch, int op);
  void fillReadyChannels(int readyEvents, CLChannelList& chs);

private:
	static const int CL_EPOLL_INIT_SIZE = 16;

private:
	typedef std::vector<struct epoll_event> EventVector;
	typedef std::map<int, CLChannel*> ChannelMap;
	ChannelMap m_channels;
	// For performance concern, use m_readyEvents, 
  // instead of init a vector of struct epoll_event 
  // every time we do doPoll
	EventVector m_readyEvents; 
  int m_epollfd;
};

} // neo

#endif //_CL_EPOLL_POLLER_HXX_

